package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.GcgWsjd;
import cn.stylefeng.guns.sys.modular.system.mapper.GcgWsjdShenpiMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.WsjdMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.GcgWsjdParam;
import cn.stylefeng.guns.sys.modular.system.model.result.GcgWsjdResult;
import cn.stylefeng.guns.sys.modular.system.service.WsjdService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 网上建店信息gcg_wsjd 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-06-15
 */
@Service
public class WsjdServiceImpl extends ServiceImpl<WsjdMapper, GcgWsjd> implements WsjdService {

    @Autowired
    private GcgWsjdShenpiMapper gcgWsjdShenpiMapper;

    @Override
    public void add(GcgWsjdParam param){
        GcgWsjd entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public int delete(GcgWsjdParam param){
        //查询是否已经审批
        int count = gcgWsjdShenpiMapper.getCon(param.getGcgWsjdId());
        if (count <= 0){
            this.removeById(getKey(param));
            return 1;
        }
        return 2;
    }

    @Override
    public void update(GcgWsjdParam param){
        GcgWsjd oldEntity = getOldEntity(param);
        GcgWsjd newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GcgWsjdResult findBySpec(GcgWsjdParam param){
        return null;
    }

    @Override
    public List<GcgWsjdResult> findListBySpec(GcgWsjdParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GcgWsjdParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public GcgWsjdResult getById(Long gcgWsjdId) {
        return this.baseMapper.getById(gcgWsjdId);
    }

    private Serializable getKey(GcgWsjdParam param){
        return param.getGcgWsjdId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GcgWsjd getOldEntity(GcgWsjdParam param) {
        return this.getById(getKey(param));
    }

    private GcgWsjd getEntity(GcgWsjdParam param) {
        GcgWsjd entity = new GcgWsjd();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
